<template>
  <!-- 容器区域 -->
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div class="">
        <img src="../assets/logo.png" />
        <span>后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 子容器区域 -->
    <el-container>
      <!-- 侧边栏区域 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">|||</div>
        <!-- 侧边栏菜单区域 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409bff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <!-- 一级菜单区域 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menulist"
            :key="item.id"
          >
            <!-- 一级菜单模板区域 -->
            <template slot="title">
              <!-- 一级菜单icon -->
              <i :class="iconsObj[item.id]"></i>
              <!-- 一级菜单描述 -->
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单区域 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState('/' + subItem.path)"
            >
              <template slot="title">
                <!-- 二级菜单icon -->
                <i class="el-icon-menu"></i>
                <!-- 二级菜单描述 -->
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 主体区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      // 左侧菜单数据
      menulist: [],
      // 一级菜单图标
      iconsObj: {
        125: 'el-icon-s-custom',
        103: 'el-icon-box',
        101: 'el-icon-shopping-bag-1',
        102: 'el-icon-s-order',
        145: 'el-icon-s-marketing'
      },
      // 是否折叠菜单
      isCollapse: false,
      // 被激活的地址
      activePath: ''
    }
  },
  methods: {
    // 退出登录
    logout() {
      window.sessionStorage.clear()
      this.$router.push('/login')
    },
    // 获取所有菜单数据
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(this.meta.msg)
      this.menulist = res.data
    },
    // 点击将菜单折叠或者展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 获取当前点击的二级菜单路由值，以供二级菜单高亮显示
    // 但是不能直接这么传值给实例，而是要复制到sessionStorage中
    // 因为页面刷新之后还是不会亮
    saveNavState(navPath) {
      window.sessionStorage.setItem('activePath', navPath)
      this.activePath = window.sessionStorage.getItem('activePath')
    }
  },
  // 要在页面加载时就开始获取数据，所以使用created
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  }
}
</script>

<style lang='less' scpoed>
.home-container {
  height: 100%;
}
.el-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #373d41;
  color: #fff;
  div {
    display: flex;
    align-items: center;
    img {
      width: 60px;
      height: 60px;
    }
    span {
      padding: 0 20px;
    }
  }
}
.el-aside {
  background-color: #333744;
  .toggle-button {
    height: 24px;
    background-color: #4a5064;
    color: #fff;
    font-size: 10px;
    line-height: 24px;
    text-align: center;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
  .el-menu {
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
</style>
